package mop.controller;

import java.util.LinkedList;
import java.util.List;

import mop.exception.IllegalDescriptionException;
import mop.exception.IllegalDuplicationException;
import mop.exception.IllegalIdException;
import mop.model.World;
import mop.model.resource.Resource;
import mop.model.resource.ResourceInterface;
import mop.model.resource.ResourceType;
import mop.model.resource.ResourceTypeInterface;

/**
 * CreateResourceController
 * 
 * Use case controller for the creation of a new resource. 
 * (use case 8)
 * 
 * @author	MOP Team 7
 */
public class CreateResourceController extends UseCaseController
{

	/**
	 * @see UseCaseController#UseCaseController(World)
	 */
	public CreateResourceController(World world)
	{
		super(world);
	}
	
	/**
	 * Get all the possible resource types
	 * 
	 * @return list with interface to all existing resource types
	 */
	public List<ResourceTypeInterface> getResourceTypes()
	{
		List<ResourceType> base = super.getWorld().getResourceTypeDataBase().fetchAll();
		if(base == null) return null;
		
		List<ResourceTypeInterface> ret = new LinkedList<ResourceTypeInterface>();
		for(ResourceType t : base)
		{
			ret.add(t);
		}
		return ret;
	}
	
	
	/**
	 * Use case : Create Resource
	 * 
	 * @param   rti
	 *			Resource Type interface 
	 * @param	description
	 * 			The description of the new Resource.
	 * @param	type
	 * 			A string representing the type of the Resource.
	 * @effect	A Resource object is made with the ResourceFactory.
	 * 			| ResourceFactory.createResource(description, type)
	 * @post	The Resource is added to the Resource database.
	 * 			| resourceDatabase.contains(this)
	 * @throws	IllegalArgumentException
	 * 			Thrown when the given resource type is invalid.
	 * @throws  IllegalDescriptionException
	 * 			Thrown when the Resource description is invalid.
	 * @throws 	IllegalDuplicationException 
	 * 			Thrown when a Resource with the same description already exists in the database.
	 */
	public ResourceInterface createResource(ResourceTypeInterface rti,String description) 
		throws IllegalDescriptionException, IllegalDuplicationException, 
		IllegalArgumentException,IllegalIdException
	{
		ResourceType rt = super.getWorld().getResourceTypeDataBase().find(rti.getDescription());
		Resource newResource = new Resource(rt,super.getWorld().getResourceDataBase(),description);
		return newResource;
	}

        public List<ResourceInterface> getAllResources()
        {
            return super.getAllResources();
        }

	/***************************************************************************
	 * 								Visit
	 ***************************************************************************/
	
	/**
	 * Make a CallBack to the visitor
	 * @param v the visitor
	 */
	public void visit(ControllerVisitor v)
	{
		v.action(this);
	}
}